package com.liangsui.mss.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.liangsui.mss.constants.Constants;
import com.liangsui.mss.domain.Dept;
import com.liangsui.mss.dto.DeptDto;
import com.liangsui.mss.mapper.DeptMapper;
import com.liangsui.mss.service.DeptService;
import com.liangsui.mss.vo.DataGridView;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;

/**
* @author zhang
* @description 针对表【sys_dept(部门/科室表)】的数据库操作Service实现
* @createDate 2022-12-05 15:54:42
*/
@Service
public class DeptServiceImpl extends ServiceImpl<DeptMapper, Dept> implements DeptService{

    @Autowired
    private DeptMapper deptMapper;

    /**
     * @param deptDto
     * 功能描述: 用于分页查询科室数据 查询可用的
     */
    @Override
    public DataGridView listPage(DeptDto deptDto) {
        Page<Dept> deptPage = new Page<>(deptDto.getPageNum(), deptDto.getPageSize());
        QueryWrapper<Dept> qw = new QueryWrapper<>();
        qw.like(StringUtils.isNotBlank(deptDto.getDeptName()),
                Dept.COL_DEPT_NAME, deptDto.getDeptName());
        qw.eq(StringUtils.isNotBlank(deptDto.getStatus()),
                Dept.COL_STATUS, deptDto.getStatus());
        qw.ge(deptDto.getBeginTime() != null,
                Dept.COL_CREATE_TIME, deptDto.getBeginTime());
        qw.le(deptDto.getEndTime() != null,
                Dept.COL_CREATE_TIME, deptDto.getEndTime());

        qw.orderByAsc(Dept.COL_ORDER_NUM);
        this.deptMapper.selectPage(deptPage, qw);
        return new DataGridView(deptPage.getTotal(), deptPage.getRecords());
    }

    /**
     * 功能描述: 查询所有数据 不分页
     */
    @Override
    public List<Dept> listDept() {
        QueryWrapper<Dept> qw = new QueryWrapper<>();
        qw.eq(Dept.COL_STATUS, Constants.STATUS_TRUE);
        qw.orderByAsc(Dept.COL_ORDER_NUM);
        return this.deptMapper.selectList(qw);
    }

    /**
     * @param id
     * 功能描述: 根据部门ID 查询信息
     */
    @Override
    public Dept getDeptById(Long id) {
        return this.deptMapper.selectById(id);
    }

    /**
     * @param deptDto
     * 功能描述: 更新部门
     */
    @Override
    public int updateDept(DeptDto deptDto) {
        Dept dept = new Dept();
        BeanUtil.copyProperties(deptDto,dept);
        // 设置修改者
        dept.setUpdateBy(deptDto.getSimpleUser().getUserName());
        return this.deptMapper.updateById(dept);
    }

    /**
     * @param deptDto
     * 功能描述: 添加部门
     */
    @Override
    public int addDept(DeptDto deptDto) {
        Dept dept = new Dept();
        BeanUtil.copyProperties(deptDto,dept);
        // 创建者、时间
        dept.setUpdateBy(deptDto.getSimpleUser().getUserName());
        dept.setCreateTime(DateUtil.date());
        return this.deptMapper.insert(dept);
    }

    /**
     * @param ids
     * 功能描述: 批量删除
     */
    @Override
    public int deleteDeptByIds(Long[] ids) {
        if (null != ids && ids.length > 0)
            return this.deptMapper.deleteBatchIds(Arrays.asList(ids));
        return 0;
    }

    @Override
    public List<Dept> listDeptByDeptIds(List<Long> deptIds) {
        if (deptIds != null && deptIds.size() > 0) {
            QueryWrapper<Dept> qw = new QueryWrapper<>();
            qw.in(Dept.COL_DEPT_ID,deptIds);
            return this.deptMapper.selectList(qw);
        }
        return Constants.EMPTY_LIST;
    }

    @Override
    public void updateDeptRegNumber(Long deptId, Integer regNumber) {
        Dept dept = new Dept();
        dept.setDeptId(deptId);
        dept.setRegNumber(regNumber);
        this.deptMapper.updateById(dept);
    }
}




